//
//  Date+Ex.swift
//  SHCountDown
//
//  Created by jiaxing liu on 2019/1/25.
//  Copyright © 2019 jiaxing liu. All rights reserved.
//

import Foundation

extension Double {
    // MARK - 时间戳转Date
    func timeStampToDate() -> Date {
        let date = Date.init(timeIntervalSince1970: self)
        let zone = NSTimeZone.system
        let interval = zone.secondsFromGMT(for: date)
        let localDate = date.addingTimeInterval(TimeInterval(interval))
        return localDate
    }
    
    
}

extension String {
    // MARK - 返回不为otions的字符串
    static func getString(_ string : String?) -> String {
        if String.isStringNil(string){
             return ""
        } else {
            return string!
        }
        
    }
    
    // MARK - 判断字符串是否为空
    static func isStringNil(_ string: String?) -> Bool {
        guard let string = string else { return true}
        if string.count == 0 {
            return true
        }else {
            return false
        }
    }
}


extension Int {
    
    // MARK - Int 转换字符串 并保留两两位
    func keepInt(_ digits: Int = 2) -> String {
        let fm = NumberFormatter()
        fm.minimumIntegerDigits = digits
        return String.getString(fm.string(for: self))
        
    }
}


extension Date {
    // MARK - 获取当前时区Date
    static func getCurrentDate() -> Date {
        let nowDate = Date()
        let zone = NSTimeZone.system
        let interval = zone.secondsFromGMT(for: nowDate)
        let localeDate = nowDate.addingTimeInterval(TimeInterval(interval))
        return localeDate
        
    }
    
    static func dateFromString(dateString: String,formatter:String = "HH:mm:ss") -> Date? {
        let fm = DateFormatter()
        fm.dateFormat = formatter
        return fm.date(from: dateString)
        
    }
    
    static func stringFromDate(date: Date,formatter:String = "HH:mm:ss") -> String {
        let fm = DateFormatter()
        fm.dateFormat = formatter
        return fm.string(from: date)
        
    }
    
}














